function getContextPath() {
    var base = document.getElementsByTagName('base')[0];
    if (base && base.href && (base.href.length > 0)) {
        base = base.href;
    } else {
        base = document.URL;
    }
    return base.substr(0,
        base.indexOf("/", base.indexOf("/", base.indexOf("//") + 2) + 1));
}

document.write('<script src="' + getContextPath() + '/js/axiomManager.js" type="text/javascript"></script>');

var util = new Util();
var messageBox = new MessageBox();

$(function() {
	var accordion = $('#accordion');
	accordion.accordion( {
		change: function(event, ui) { 
			onChange(ui.newHeader, ui.newContent);
		},
		autoHeight: false
	});
	var childIndex = accordion.accordion( "option", "active" );
	var activeHeader = $('#accordion > .ui-accordion-header:eq(' + childIndex + ')');
	onChange(activeHeader, activeHeader.next());
});

function onChange(header, content) {
	if(!header.is('.loaded')) {
		load(header, content);
	}
}

function load(header, content, onLoad) {
	util.openWaitOverlay();
	
	var callback = null;
	var getType = null;
	if(header.is('.axiomList')) {
		if(axiomListActions === null) {
			axiomListActions = new AxiomListingActions(content);
		}
		callback = axiomListActions.load;
		getType = 'axiomList';
	} else {
		util.closeWaitOverlay();
	}
	if(onLoad) {
		callback = onLoad;
	}
	
	if(callback != null) {
		util.requestPost({
				action: 'get',
				type: getType
			},
			function(data) {
				callback(header, content, data);
				util.closeWaitOverlay();
				header.addClass('loaded');				
				header.parent('.accordion').accordion('resize');
			},
			function() {
				content.empty();
				
				util.closeWaitOverlay();
				
				messageBox.showMessage('Unexpected problem loading panel at this time.  Possibly server down?', 'Error');
			}
		);
	}
}

function Util() {
	this.refresh = function(name, content, callback) {
		var header = $('.accordion > .ui-accordion-header.' + name);
		if(!content) {
			content = header.next();
		}
		load(header, content, callback);		
	};
	
	this.requestPost = function(params, successCallback, failureCallback) {
		$.post('/containerLogic/axiom', 
				params,
				successCallback,
				'json'
		).error(failureCallback);			
	};
	
	this.openWaitOverlay = function(parent) {
		var waitOverlay = $('<div class="wait"><img src="images/wait.gif" /></div>');
		if(!parent) {
			parent = $('body');
		}
		parent.append(waitOverlay);
		waitOverlay.dialog({
			height: 230,
			width: 270,
			modal: true,
			closeOnEscape: false,
			closable: false,
			resizable: false,
		}).dialog('widget').find('.ui-dialog-titlebar').hide();	
	};

	this.closeWaitOverlay = function() {
		var waitOverlay = $('.wait');
		waitOverlay.dialog('close');
		waitOverlay.remove();	
	};
	
	this.buildSnazzyTable = function(params, data) {
		var table = '<table class="snazzyTable"><thead><tr class="ui-state-default">';
		for(var i=0; i<params.columnNames.length; i++) {
			table += '<th>' + params.columnNames[i] + '</th>';
		}
		table += '</tr></thead><tbody>';
		
		for(var index in data) {
			var obj = data[index];
			
			if(!params.skipIf || !params.skipIf(obj)) {
				table += '<tr>';
				for(var i=0; i<params.columnProperties.length; i++) {
					var columnInfo = params.columnProperties[i];
					if(typeof(columnInfo.access) === 'string') {
						var className = columnInfo.access + 'Column';
						if(columnInfo.className) {
							className = columnInfo.className;
						}					
						table += '<td class="' + className + '">' + obj[columnInfo.access] + '</td>';
					} else if (typeof(columnInfo.access) === 'function') {
						table += '<td';
						if(columnInfo.className) {
							table += ' class="' + columnInfo.className + '"';
						}
						table += '>' + columnInfo.access(obj) + '</td>';
					}
				}
				table += '</tr>';				
			}
		}
		table += '</tbody></table>';	
		return table;
	};
}

function MessageBox() {
	var div = null;
	
	this.showMessage = function(message, title) {
		div.html('<p>' + message + '</p>');
		
		if(title) {
			div.dialog('option', 'title', title);
		}
		div.dialog('open');
	};
	
	this.showContent = function(content, callback, title) {
		div.html(content);
		
		if(title) {
			div.dialog('option', 'title', title);
		}		
		div.bind('dialogbeforeclose', function() {
			var ret = callback();
			div.unbind('dialogbeforeclose');
			return ret;
		});
		div.dialog('open');
	};
	
	this.showConfirmation = function(message, callbackOnOkay, title) {
		div.html('<table><tbody><tr><td colspan="2"><p>' + message + '</p></td></tr><tr><td><span class="button ok">Okay</span></td><td><span class="button cancel">Cancel</span></td></tr></tbody></table>');
		
		div.find('.button').button();
		div.find('.ok')
			.button('option', 'icons', {primary:'ui-icon-check' })
			.click(function() {
				callbackOnOkay();
				div.dialog('close');
			});
		div.find('.cancel')
			.button('option', 'icons', {primary:'ui-icon-cancel' })
			.click(function() {
			div.dialog('close');
		});		
		
		if(title) {
			div.dialog('option', 'title', title);
		}
		div.dialog('open');		
	};
	
	this.showContentInput = function(content, callbackOnOkay, title) {
		div.html('<table><tbody><tr><td colspan="2" class="contentInsert"></td></tr><tr><td><span class="button ok">Okay</span></td><td><span class="button cancel">Cancel</span></td></tr></tbody></table>');
		
		div.find('.button').button();
		div.find('.ok')
			.button('option', 'icons', {primary:'ui-icon-check' })
			.click(function() {
				callbackOnOkay();
				div.dialog('close');
			});
		div.find('.cancel')
			.button('option', 'icons', {primary:'ui-icon-cancel' })
			.click(function() {
				div.dialog('close');
			});		
		
		div.find('.contentInsert').append(content);
		if(title) {
			div.dialog('option', 'title', title);
		}
		div.dialog('open');			
	}
	
	function init() {
		if(div === null) {
			div = $('<div class="messageBox"></div>');
			$('body').append(div);
			
			div.dialog({
				modal: true,
				width: 350,
				autoOpen: false
			})
		}
	}
	
	$(function() {
		init();		
	});
}
